use honeybee::prelude::*;
use honeybee::Result;

use crate::{apps, users};

pub struct Delegate;
#[async_trait::async_trait]
impl ListenDelegate for Delegate {
    async fn on_started(&self, stream: &mut TcpStreamActor) -> Result<()> {
        if stream.remote_node_type() == "app" {
            let apps_addr = apps::Apps::from_registry().await?;
            let _ = apps_addr.send(apps::AppConnected(
                stream.remote_node_name().into(),
                stream.self_addr(),
            ));

            let actor_center = ActorCenter::from_registry().await?;
            let users_addr: Addr<users::Users> = actor_center.call(Query::new("Users")).await?;
            let _ = users_addr.send(users::AppConnected(
                stream.remote_node_name().into(),
                stream.self_addr(),
            ));
        }
        Ok(())
    }

    async fn on_stoped(&self, _stream: &mut TcpStreamActor) -> Result<()> {
        Ok(())
    }
}
